package registnet.base.courseoffering;

import java.util.List;
import java.util.Vector;

import registnet.util.schedule.Day;
import registnet.util.schedule.Schedule;
import registnet.util.schedule.Task;

/**
 * An implementation of a course offering.
 */
public class CourseOffering extends AbstractCourseOffering{

	/**
	 * The schedule of the course offering.
	 */
	private Schedule schedule;
	
	/**
	 * The number of remaining available slots.
	 */
	private int slots;
	
	/**
	 * The list of student numbers of students enlisted in the course offering.
	 */
	private List<String> enlistedStudents;	//student number
	
	/**
	 * The rank of the course offering. Rank is processed in the enlistment process.
	 */
	private int rank;
	
	/**
	 * Default constructor.
	 */
	public CourseOffering(){
		slots = 0;
		enlistedStudents = new Vector<String>();
		rank = 0;
	}
	
	/**
	 * Put the raw schedule data of the course offering into <code>Schedule</code> object.
	 */
	public void loadSchedule(){
		schedule = new Schedule();
		List<String> rawDays = new Vector<String>();
		
		/*
		 * lecture
		 */
		if(!lectureDay.equals("TBA") && !lectureDay.equals(null) && !lectureDay.equals("")){
			rawDays = Day.tokenize(lectureDay);
			for(String day : rawDays){
				Task task = Task.processRawData(lectureTime);
				if(task!=null)
					schedule.getDays().get(Day.MAP_DAY.get(day)).addTasks(task);
			}
		}
		
		/*
		 * 	laboratory
		 */
		if(!laboratoryDay.equals("TBA") && !laboratoryDay.equals(null) && !laboratoryDay.equals("")){
			rawDays = Day.tokenize(laboratoryDay);
			for(String day : rawDays){
				Task task = Task.processRawData(laboratoryTime);
				if(task!=null)
					schedule.getDays().get(Day.MAP_DAY.get(day)).addTasks(task);
			}
		}
		
		/*
		 * 	recitation
		 */
		if(!recitationDay.equals("TBA") && !recitationDay.equals(null) && !recitationDay.equals("")){
			rawDays = Day.tokenize(recitationDay);
			for(String day : rawDays){
				Task task = Task.processRawData(recitationTime);
				if(task!=null)
					schedule.getDays().get(Day.MAP_DAY.get(day)).addTasks(task);
			}
		}
		
	}

	/**
	 * Returns the schedule of the course offering.
	 * @return the schedule of the course offering
	 */
	public Schedule getSchedule() {
		return schedule;
	}

	/**
	 * Returns the rank of the course offering.
	 * @return the rank of the course offering
	 */
	public int getRank() {
		return rank;
	}

	/**
	 * Sets the rank of the course offering.
	 * @param rank
	 */
	public void setRank(int rank) {
		this.rank = rank;
	}

	/**
	 * Returns the number of remaining available slots.
	 * @return the number of remaining available slots
	 */
	public int getSlots() {
		return slots;
	}

	/**
	 * Sets the number of remaining available slots.
	 * @param slots
	 */
	public void setSlots(int slots) {
		this.slots = slots;
	}
	
	/**
	 * Enlist a student with the given student number to the course offering.
	 * @param studentNumber
	 * @return the number of remaining available slots. Negative 1 is returned if there is an error in the enlistment process. 
	 */
	public int enlistStudent(String studentNumber){
		if(slots>0){
			enlistedStudents.add(studentNumber);
			slots = slots - 1;
			return slots;
		}else{
			System.err.println("****[error] Enlistment failed. No more slots for "+this.getCourseCode()+ "("+this.getName()+")");
			return -1;
		}
		
	}

	/**
	 * Returns a list of student numbers of students enlisted in the course offering.
	 * @return a list of student numbers of students enlisted in the course offering
	 */
	public List<String> getEnlistedStudents() {
		return enlistedStudents;
	}
	
	/**
	 * Prints basic information about the course offering.
	 */
	public void print(){
		System.out.println("\r\n[check] dao.CourseOffering: "+courseCode+" "+name);
		System.out.println("[check]        lectureDay: "+lectureDay);
		System.out.println("[check]        laboratoryDay: "+laboratoryDay);
		System.out.println("[check]        recitationDay: "+recitationDay);
		System.out.println("[check]        lectureTime: "+lectureTime);
		System.out.println("[check]        laboratoryTime: "+laboratoryTime);
		System.out.println("[check]        recitationTime: "+recitationTime);
		System.out.println("[check]        lectureRoom: "+lectureRoom);
		System.out.println("[check]        laboratoryRoom: "+laboratoryRoom);
		System.out.println("[check]        recitationRoom: "+recitationRoom);
	}
	
}
